package com.concerneyes.mine.Task;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.concerneyes.CEApp;
import com.concerneyes.R;
import com.concerneyes.mine.model.MyGlassManModel;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import common.AbsActivity;
import common.PreferUtil;
import http.HttpAsyncTask;
import http.JsonMap;
import http.Url;

/**
 * Created by Administrator on 2017/7/3 0003.
 */

public class GlassManImpl {
    private Context context;
    private GlassManListener listener;
    private AbsActivity absActivity;

    public GlassManImpl(AbsActivity absActivity, GlassManListener listener) {
        this.absActivity = absActivity;
        this.context = CEApp.getInstance();
        this.listener = listener;
    }

    /**
     * 配戴人列表
     */
    public void listGlassMan() {
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("uid", String.valueOf(PreferUtil.getUserId()));
        HttpAsyncTask task = new HttpAsyncTask() {
            @Override
            public void onStart(String arg0) {
                absActivity.showLoadingDialog(R.string.loading);
            }

            @Override
            public void onFinish(String arg0, String arg1) {
                Log.e(this.getClass().getSimpleName(), "arg1==>" + arg1);
                absActivity.cancelLoadingDialog();
                JsonMap ret = JsonMap.parseJson(arg1);
                List<MyGlassManModel> model = null;
                if (TextUtils.isEmpty(arg1)) {
                    absActivity.showToast(R.string.network_error_tip);
                    return;
                }
                try {
                    if (ret.getInt("code") == 200) {
                        JSONObject object = new JSONObject(arg1);
                        String str = object.getString("data");
                        model = new Gson().fromJson(str, new TypeToken<ArrayList<MyGlassManModel>>() {
                        }.getType());
                    } else {
                        absActivity.showToast(ret.getString("msg"));
                        listener.glassManListFail();
                    }
                } catch (Exception e) {
                }
                if (model != null) {
                    listener.glassManListSuccess(model);
                } else {
                    listener.glassManListFail();
                }
            }
        };
        task.setDebug(true);
        task.excute(Url.BaseUrl + "/glassMan/list", params, "POST");
    }

    /**
     * 删除配戴人
     *
     * @param id 对应配戴人的ID
     */
    public void deteleGlassMan(int id) {
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("id", String.valueOf(id));
        params.put("uid", PreferUtil.getUserId());
        HttpAsyncTask task = new HttpAsyncTask() {

            @Override
            public void onStart(String arg0) {
                absActivity.showLoadingDialog(R.string.loading);
            }

            @Override
            public void onFinish(String arg0, String arg1) {
                absActivity.cancelLoadingDialog();
                JsonMap ret = JsonMap.parseJson(arg1);
                List<MyGlassManModel> model = null;
                try {
                    if (ret.getInt("code") == 200) {
                        JSONObject object = new JSONObject(arg1);
                        String str = object.getString("data");
                        model = new Gson().fromJson(str, new TypeToken<ArrayList<MyGlassManModel>>() {
                        }.getType());
                    } else {
                        absActivity.showToast(ret.getString("msg"));
                        listener.deleteGlassManFail();
                    }
                } catch (Exception e) {
                }
                if (model != null) {
                    listener.deleteGlassManSuccess(model);
                } else {
                    listener.deleteGlassManFail();
                }
            }
        };
        task.setDebug(true);
        task.excute(Url.BaseUrl + "/glassMan/delete", params, "POST");
    }

    /**
     * 增加或修改配戴人
     *
     * @param isAdd          是否为新增配戴人
     * @param id             需要修改的配戴人id
     * @param year
     * @param month
     * @param day
     * @param realName
     * @param gender
     * @param driveFrequency 开车频率
     * @param isUseComputer  使用电脑的频率
     * @param job            职业
     */
    public void addGlassMan(boolean isAdd, String id, String year, String month,
                            String day, String realName, String gender,
                            String driveFrequency, boolean isUseComputer, String job) {
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("uid", String.valueOf(PreferUtil.getUserId()));
        if (!TextUtils.isEmpty(realName)) {
            params.put("realName", realName);
        }
        if (!TextUtils.isEmpty(gender)) {
        params.put("gender", gender);
        }
        if (!TextUtils.isEmpty(year)) {
        params.put("year", year);
        }
        if (!TextUtils.isEmpty(month)) {
        params.put("month", month);
        }
        if (!TextUtils.isEmpty(day)) {
        params.put("day", day);
        }
        if (!TextUtils.isEmpty(job)) {
        params.put("job", job);
        }
        if (!TextUtils.isEmpty(driveFrequency)) {
        params.put("driveFrequency", driveFrequency);
        }
        if (!TextUtils.isEmpty(gender)) {
        params.put("isUseComputer", String.valueOf(isUseComputer));
        }
        if (!isAdd) {
            params.put("id", id);
        }
        Log.e("params", "" + params);
        HttpAsyncTask task = new HttpAsyncTask() {
            @Override
            public void onStart(String arg0) {
                absActivity.showLoadingDialog(R.string.loading);
            }

            @Override
            public void onFinish(String arg0, String arg1) {
                absActivity.cancelLoadingDialog();
                Log.e("arg1", "" + arg1);
                JsonMap ret = JsonMap.parseJson(arg1);
                if (TextUtils.isEmpty(arg1)) {
                    listener.addGlassManFail();
                    return;
                }
                try {
                    if (ret.getInt("code") != 200) {
                        absActivity.showToast(ret.getString("msg"));
                        listener.addGlassManFail();
                    } else {
                        listener.addGlassManSuccess();
                    }
                } catch (Exception e) {
                    listener.addGlassManFail();
                }
            }
        };
        task.setDebug(true);
        if (isAdd) {
            task.excute(Url.BaseUrl + "/glassMan/add", params, "POST");
        } else {
            task.excute(Url.BaseUrl + "/glassMan/update", params, "POST");
        }

    }
}
